<!--
* @description:新增部门
* @fileName: TheNewDepartment
* @author:孟帅龙
* @date:2021-1-13
* @后台人员:孟帅龙
-->
<template>
  <div>
    <!-- 新增部门 -->
    <div class="xzbm">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- <el-form-item label="编号：" prop="DeptNumber">
          <el-input
            v-model="ruleForm.DeptNumber"
            placeholder="编号一般由bm+4位数组成，如bm0001"
          ></el-input>
        </el-form-item> -->
        <el-form-item label="部门名称：" prop="DeptName">
          <el-input v-model="ruleForm.DeptName"></el-input>
        </el-form-item>
        <el-form-item label="上级名称：" prop="SuperiorDept">
          <el-select
            v-model="ruleForm.SuperiorDept"
            placeholder="请选择上级名称"
          >
            <el-option label="——" value="——"></el-option>
            <el-option label="总经理" value="总经理"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        // DeptNumber:"",
        DeptName:"",
        SuperiorDept:"",
      },
      rules: {
        DeptName: [
          { required: true, message: "请输入部门名称", trigger: "blur" },
        ],
        SuperiorDept: [
          { required: true, message: "请选择上级名称", trigger: "change" },
        ],
        DeptNumber: [
            { required: true, message: '请输入编号', trigger: 'blur' },
            { min: 6, max: 6, message: '长度必须为6个字符', trigger: 'blur' }
          ],
      },
    };
  },
  methods: {
    goBack() {
      this.$router.push("/DivisionManagement");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          this.$axios.post('https://localhost:44399/api/app/department/add-department',this.ruleForm).then(res=>{
            if(res.data>0){
              this.$message.success('添加成功');
              this.$router.push("/DivisionManagement");
            }else{
              this.$message.error('添加失败');
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.xzbm {
  width: 500px;
}
</style>